/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import sample.web.model.User;
import sample.web.service.UserService;
import sample.web.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping(value = "login",method = RequestMethod.GET)
	public String login() {
		return "login";
	}

    @RequestMapping(value = "login",method = RequestMethod.POST)
	public String login(Map<String, Object> model,HttpServletRequest request, @ModelAttribute("user") User user) {
		model.put("user",user);
        request.getSession().setAttribute("user",user);
		int roleid=userService.getUserByLoginInfo(user);
		request.getSession().setAttribute("roleid",roleid);
		if(roleid!=0){
			System.out.println("登录成功!");
			return "redirect:/index";
		} else{
			System.out.println("不存在该用户!");
			return "error";
		}
	}
	@RequestMapping(value = "regin",method = RequestMethod.GET)
	public String regin() {
		return "regin";
	}

	@RequestMapping(value = "regin",method = RequestMethod.POST)
	public String regin(Map<String, Object> model,HttpServletRequest request, @ModelAttribute("user") User user,RedirectAttributes redirectAttributes) {
		model.put("user",user);
		userService.saveByReginInfo(user);
		redirectAttributes.addFlashAttribute("username",user.getUsername());
		redirectAttributes.addFlashAttribute("message","注册成功,请登录!");
		return WebUtil.redirect("/user/login");
	}

	@RequestMapping(value = "add",method = RequestMethod.GET)
	public String add() {
		System.out.println("add page");
		return "/user/add";
	}

	@RequestMapping(value = "edit",method = RequestMethod.GET)
	public String edit() {
		return "/user/edit";
	}

	@RequestMapping(value = "logout",method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return WebUtil.redirect("/user/login");
	}

	@RequestMapping(value = "noFound",method = RequestMethod.GET)
	public String noFound() {
		return "/user/noFound";
	}




}
